/**
* HSBC Edit settings Page
*
* This page will render the GUI and the HSBC settings feilds. This page is
* commented extensively as the hsbc code is used to base many payment modules
*
* @access 	public
* @author 	Rob
* @copyright	ResMania 2009 all rights reserved.
* @version	1.0
* @link		http://developer.resmania.com/docs:creating_resmania_plugins
* @since  	05-2009
*/

/**
* RM.Pages.Plugin_HSBC_Config_Toolbar
*
* Setup the HSBC Edit Page toolbar Buttons. This function will render the toolbar
* to the upper right of the GUI. For this page we just need one button to save settings.
*
* @return   ExtJS Panel
*/
RM.Pages.Plugin_HSBC_Config_Toolbar = {
    xtype : "panel",
    id : "rm_pages_plugins_hsbc_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([{image: RM.BaseLargeImageURL+"save.gif", label: RM.Translate.Admin.HSBC.Main.Save, link: "RM.Pages.Functions.plugins_hsbc_save()"}])
}
RM.Toolbars.push(RM.Pages.Plugin_HSBC_Config_Toolbar);

/**
* RM.Pages.Functions.plugins_hsbc_save
*
*/
RM.Pages.Functions.plugins_hsbc_save = function(){
    RM.Pages.Plugin_HSBC_Panel.getForm().submit({
        params: {
            'id' : Ext.getDom('rm_pages_plugins_hsbc_id').value,
            'client_id' : Ext.getDom('rm_pages_plugins_hsbc_client_id').value,
            'user_id' : Ext.getDom('rm_pages_plugins_hsbc_user_id').value,
            'password' : Ext.getDom('rm_pages_plugins_hsbc_password').value,
            'preauth_mode' : Ext.getDom('rm_pages_plugins_hsbc[preauth_mode]').value,
            'defaultplugin' : Ext.getDom('rm_pages_plugins_hsbc[defaultplugin]').value
        },
        success: function(form, action) {
            RM.Pages.HSBC_StatusBar = Ext.getCmp('rm_pages_plugins_hsbc_statusbar');
            RM.Pages.HSBC_StatusBar.setStatus({
                text: RM.Translate.Admin.HSBC.Main.SaveOK,
                iconCls: 'ok-icon'
            });
        },
        failure: function() {
            RM.Pages.HSBC_StatusBar = Ext.getCmp('rm_pages_plugins_hsbc_statusbar');
            RM.Pages.HSBC_StatusBar.setStatus({
                text: RM.Translate.Admin.HSBC.Main.SaveFailed,
                iconCls: 'failed-icon'
            });
        },
        waitMsg: RM.Translate.Common.Saving,
        waitTitle: RM.Translate.Common.PleaseWait
    });
}


// the hidden ID feild use to connect this to the DB record.
RM.Pages.Plugin_HSBC_ID_Field = new Ext.form.Hidden({
    id : "rm_pages_plugins_hsbc_id",
    value: 1
});

// the clientid id feild
RM.Pages.Plugin_HSBC_ClientID_Field = new Ext.form.TextField({
    fieldLabel: RM.Translate.Admin.HSBC.Main.ClientID,
    id : "rm_pages_plugins_hsbc_client_id",
    width : 200
});

// the username key feild
RM.Pages.Plugin_HSBC_Username_Field = new Ext.form.TextField({
    fieldLabel: RM.Translate.Admin.HSBC.Main.UserID,
    id : "rm_pages_plugins_hsbc_user_id",
    width : 200
});

// the password key feild
RM.Pages.Plugin_HSBC_Password_Field = new Ext.form.TextField({
    fieldLabel: RM.Translate.Admin.HSBC.Main.Password,
    id : "rm_pages_plugins_hsbc_password",
    inputType: 'password',
    width : 200
});

// the pre-authorize payments account checkbox
RM.Pages.Plugin_HSBC_PreAuth_Selection = {
    id: "rm_pages_plugins_hsbc[preauth_mode]",
    name: "rm_pages_plugins_hsbc_preauth_mode",
    inputValue: "1",
    fieldLabel: RM.Translate.Admin.HSBC.Main.PreAuth,
    xtype: "xcheckbox"
};

// the default plugin checkbox
RM.Pages.Plugin_HSBC_Default_Selection = {
    id: "rm_pages_plugins_hsbc[defaultplugin]",
    name: "rm_pages_plugins_hsbc_defaultplugin",
    inputValue: "1",
    fieldLabel: RM.Translate.Admin.HSBC.Main.Default,
    xtype: "xcheckbox"
};

// get account message
RM.Pages.Plugin_HSBC_GetAccountText = new Ext.Panel({
    fieldLabel: '',
    html: '<div id="rm_pages_admin_form_textlink"><a href="https://www.hsbc.com/" target="_blank">'+RM.Translate.Admin.HSBC.Main.GetAccount+'</a></div>',
    bodyBorder : false
});


// feildset
RM.Pages.Plugin_HSBC_Fieldset = new Ext.form.FieldSet({
    id: "rm_pages_plugins_hsbc_fieldset",
    title: RM.Translate.Admin.HSBC.Main.Settings,
    autoHeight: true,
    layout: 'form',
    bodyBorder : false,
    items: [
        RM.Pages.Plugin_HSBC_ID_Field,
        RM.Pages.Plugin_HSBC_ClientID_Field,
        RM.Pages.Plugin_HSBC_Username_Field,
        RM.Pages.Plugin_HSBC_Password_Field,
        RM.Pages.Plugin_HSBC_PreAuth_Selection,
        RM.Pages.Plugin_HSBC_Default_Selection
    ]
});


RM.Pages.Functions.HSBC_ConfigJson = function (responseObject) {

    var myMask = new Ext.LoadMask('rm_pages_plugins_hsbc_main_form', {
        msg: RM.Translate.Common.PleaseWait,
        msgCls: "RM_Loading_Mask_Msg_Hidden" // hide the msg
    });
    myMask.show();

    var settings = responseObject.settings[0];

    // set the values of the form items
    Ext.getCmp("rm_pages_plugins_hsbc_id").setValue(settings.id);
    Ext.getCmp("rm_pages_plugins_hsbc_client_id").setValue(settings.client_id);
    Ext.getCmp("rm_pages_plugins_hsbc_user_id").setValue(settings.user_id);
    Ext.getCmp("rm_pages_plugins_hsbc_password").setValue(settings.password);
    Ext.getCmp("rm_pages_plugins_hsbc[preauth_mode]").setValue(settings.preauth_mode);
    Ext.getCmp("rm_pages_plugins_hsbc[defaultplugin]").setValue(settings.defaultplugin);

    Ext.getCmp('rm_pages_plugins_hsbc_main_form').render();
    Ext.getCmp('rm_pages_plugins_hsbc_main_form').doLayout();

    Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_plugins_hsbc_toolbar');
    Ext.getCmp('content-panel').layout.setActiveItem('rm_pages_plugins_hsbc_main_form');

    myMask.hide();

    RM.Help.Load('Admin.HSBC.Edit.Main');

}

// main panel
RM.Pages.Plugin_HSBC_Panel = new Ext.FormPanel({
    id : 'rm_pages_plugins_hsbc_main_form',
    title: RM.Translate.Admin.HSBC.Main.Title,
    bodyStyle : "padding:10px;",
    items : [
        RM.Pages.Plugin_HSBC_Fieldset
    ],
    url : RM.Common.AssembleURL({
        controller: 'HSBC',
        action: 'updateJson'
    }),
    bbar: new Ext.ux.StatusBar({
        id: 'rm_pages_plugins_hsbc_statusbar',
        items: []
    })
});

RM.Main.Pages.push(RM.Pages.Plugin_HSBC_Panel);